#encoding=utf-8
__author__ = 'Soul'

from core.PageFetcher import *


class ChinaCarPageFetcher(PageFetcher):
    BASE_XPATH = "//table//tr[%s]/td[%s]"
    BASE_ROW_XPATH = "//table//tr[%s]"

    def __init__(self):
        self.__html_src = ''
        self.__db_key__ = "chinacardb"
        pass

    def preFectch(self, params, headers):
        #import time
        self.__url__ = self.__url__
        referer_url = self.__url__.replace("search_param.asp", "search_view.asp")

        if headers == None: headers = {}

        #headers['Cookie'] = "ASPSESSIONIDASQRBASQ=OBGBIEJCANDCHJOFFEACDIJL;"
        headers['Accept-Language'] = 'en-US,en;q=0.8,zh-CN;q=0.6,zh-TW;q=0.4'
        headers['Accept-Encoding'] = 'gzip, deflate, sdch'
        headers['Referer'] = referer_url
        headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36'
        headers['X-Requested-With'] = 'XMLHttpRequest'
        headers['Accept'] = 'text/html, */*'
        headers['Connection'] = 'keep-alive'
        headers['host'] = 'www.chinacar.com.cn'

    def on_get_raw_html_src(self):
        # import gzip
        # import StringIO
        # data = StringIO.StringIO(self.__html_src__)
        # gzipper = gzip.GzipFile(fileobj=data)
        # self.__html_src__ = gzipper.read()
        return

    def __safe_get_value(self, val):
        ret = ''
        if isinstance(val, list):
            if len(val) > 0:
                ret = val[0]
            else:
                ret = ""

        if not isinstance(ret, str):
            try:
                ret = ret.text
            except Exception as err:
                print err
                ret = ""

        if not ret:
            ret = ""

        return str(ret.encode("utf8"))

    def __safe_get_tarid(self):
        urllist = self.__url__.split("?")
        if len(urllist) > 1:
            params = urllist[1]
            keyval = params.split("=")
            if len(keyval) > 1:
                return keyval[1]

        return ''

    def __get_table_keyval_pair_list(self, parsed_html, startrow, endrow, startcol = 1, endcol = 4):
        kv_list = []
        for row_idx in range(startrow, endrow + 1):
            for col_idx in range(startcol, endcol + 1, 2):
                try:
                    key_path = self.BASE_XPATH % (row_idx, col_idx)
                    key = parsed_html.xpath(key_path)
                    key = self.__safe_get_value(key)
                    val_path = self.BASE_XPATH % (row_idx, col_idx + 1)
                    val = parsed_html.xpath(val_path)
                    val = self.__safe_get_value(val)

                    if key == "": continue

                    kv_list.append({"key": key, "value": val})
                except Exception as err:
                    continue

        return kv_list

    def __get_table_rowval(self, parsed_html, row):
        kv_list = []
        try:
            val_path = self.BASE_ROW_XPATH % (row)
            val = parsed_html.xpath(val_path)
            val = self.__safe_get_value(val)
            kv_list.append({"key": "key", "value": val})
        except Exception as err:
            print err

        return kv_list

    def analysis(self):
        import json
        from lxml.html import fromstring

        parsed_html = fromstring(self.__html_src__)

        json_dict = {}
        # 生产企业信息 tr:2 ~ 9, td: 1 ~ 4
        entrp_list = self.__get_table_keyval_pair_list(parsed_html, 2, 9)
        #entrp_list_str = json.dumps(entrp_list)
        json_dict['entrp_list'] = entrp_list

        # 免检说明 tr: 11, td: 1 ~ 4
        mj_list = self.__get_table_keyval_pair_list(parsed_html, 11, 11)
        #mj_list_str = json.dumps(mj_list)
        json_dict['mj_list'] = mj_list

        # 公告状态 tr: 13, td: 1 ~ 4
        gg_list = self.__get_table_keyval_pair_list(parsed_html, 13, 13)
        #gg_list_str = json.dumps(gg_list)
        json_dict['gg_list'] = gg_list

        # 主要技术参数: tr: 16 ~ 32, td: 1 ~ 4
        jjcs_list = self.__get_table_keyval_pair_list(parsed_html, 16, 32)
        #jjcs_list_str = json.dumps(jjcs_list)
        json_dict['jjcs_list'] = jjcs_list

        # 发动机参数: tr: 34 ~ 35, td: 1 ~ 4
        fdjcs_list = self.__get_table_keyval_pair_list(parsed_html, 34, 35)
        #fdjcs_list_str = json.dumps(fdjcs_list)
        json_dict['fdjcs_list'] = fdjcs_list

        # 车辆燃料参数: tr: 37 ~ 38, td: 1 ~ 4
        clrl_list = self.__get_table_keyval_pair_list(parsed_html, 37, 38)
        #clrl_list_str = json.dumps(clrl_list)
        json_dict['clrl_list'] = clrl_list

        # 其他: tr: 40
        otherinfo_list = self.__get_table_rowval(parsed_html, 40)
        #otherinfo_list_str = json.dumps(otherinfo_list)
        json_dict['otherinfo_list'] = otherinfo_list

        # 反光标识参数： 42 ~ 43
        fgcs_list = self.__get_table_keyval_pair_list(parsed_html, 42, 43)
        #fgcs_list_str = json.dumps(fgcs_list)
        json_dict['fgcs_list'] = fgcs_list

        json_info = json.dumps(json_dict,ensure_ascii=False)
        # insert the json strings to db, column as cecar_jsoninfo
        tarid = self.__safe_get_tarid()
        if tarid:
            from dboperater.DbOperatorFactory import *
            factory = DbOperatorFactory()
            operator = factory.GetDbOperator(self.__db_key__)
            operator.update_detail(tarid, json_info)